angular.module('pook', [
    'templates'
    'constants'
    'ui.router'
    'ngAnimate'
    'ngSanitize'
    'ngStorage'
    'ui.bootstrap'
    'easypiechart'
    'textAngular'
    'ui.tree'
    'ngMap'
    'ngTagsInput'
    'slick'
    'selectApp'
    'ui.calendar'
    'tablesorting'
    'inlineedittable'
    'TodoApp'
    'angular-animate'
    'app.controllers'
    'app.directives'
    'app.localization'
    'app.nav'
    'app.ui.ctrls'
    'app.ui.directives'
    'app.form.validation'
    'app.ui.form.ctrls'
    'app.ui.form.directives'
    'app.tables'
    'app.task'
    'app.chart.ctrl'
    'app.chart.directives'
    'app.calendar'
    'app.ui.jvectormap'
    'xeditable'
    'FullscreenApp'
    'galleryApp'
    'datatables'
    'wf-utils'
    'Controller'
    'rzModule'
])
.config(($provide, userDataProvider, urls,$stateProvider, $httpProvider)->
    sys_backend=urls.sys_backend;
    backend = urls.backend;
    $httpProvider.interceptors.push ($q)->
        request: (config)->
#            config.headers['Content-Type']='application/json';
#            config.headers['Access-Control-Allow-Origin']='*';
#            config.headers['Access-Control-Allow-Methods']='GET, POST, PUT, DELETE, OPTIONS';
#            config.headers['Access-Control-Request-Headers']='Content-Type,Authorization';
            config
        responseError: (err)->
            console.log('responseErr',err)
            if(401 == err.status || -1 ==err.status)#token过期，重新登录
                userDataProvider.$get().login(true)
            return $q.reject(err)
    if (backend!=undefined && sys_backend!=undefined)
        httpDecorator =($delegate)->
            return (method, url, post, callback, headers, timeout, withCredentials)->
                token = userDataProvider.$get().getToken()
                if (token != undefined && token != null && url.indexOf('/login')==-1)
                    if(url.indexOf('?')>-1)
                        url=url+'&token='+token
                    else
                        url=url+'?token='+token
                #                    headers.Authorization = token
                if (url != null && url != undefined && url.charAt(0) in ['/','@'])
                    if url.indexOf('@ostengar')!=-1
                        url=sys_backend + url.replace('@ostengar','')
                    else
                        url = backend + url
                $delegate(method, url, post, callback, headers, timeout, withCredentials)
        $provide.decorator('$httpBackend', ["$delegate", httpDecorator])
)
.run () ->
    $url = location.href
    $qIndex = $url.indexOf('?')
    $hashIndex = $url.indexOf('#')
    if $qIndex < $hashIndex
        $urlArr = $url.substr($qIndex + 1, $hashIndex - $qIndex - 1).split('&')
    else
        $urlArr = $url.substr($qIndex + 1).split('&')
    $params = {}
    for $str in $urlArr
        $key = $str.split('=')[0]
        $value = $str.split('=')[1]
        $params[$key] = $value



angular.module('wf-utils',[])
angular.module('Controller',[])

